package com.macrolab.secure.softhsm;

import org.bouncycastle.jce.PKCS10CertificationRequest;
import sun.security.pkcs11.SunPKCS11;

import javax.security.auth.x500.X500Principal;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.*;

public class My1 {

    public static void main(String[] args) throws Exception {
        // Set up the Sun PKCS 11 provider
        String configName = "D:\\my\\workspace\\softhsm-1\\src\\main\\resources\\softhsm.cfg";

        Provider p = new SunPKCS11(configName); // softHSM 连接配置

        if (-1 == Security.addProvider(p)) {
            throw new RuntimeException("could not add security provider");
        }

        // C:\ >  softhsm2-util.exe --init-token --slot 0 --label "My token 1"
        // os pin: 1qaz
        // user pin: 2wsx
        String userPin = "2wsx";
        // Load the key store
        KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
        keyStore.load(null, userPin.toCharArray());  // 连接softHSM
        System.out.println("keyStore Type: " + keyStore.getType());
        System.out.println("keyStore Provider: " + keyStore.getProvider());
        System.out.println("keyStore Provider name: " + keyStore.getProvider().getName());
        System.out.println("keyStore Provider info: " + keyStore.getProvider().getInfo());

        System.out.println("--- keyStore Provider elements begin ------------------------------ ");
        Set<Map.Entry<Object, Object>> entries = keyStore.getProvider().entrySet();
        entries.stream().forEach(e -> {
            System.out.println(e.getKey() + " : " + e.getValue());
        });
        System.out.println("-- keyStore Provider elements end ------------------------------ ");


        System.out.println("-- keyStore begin ------------------------------ ");
        Enumeration enumeration = keyStore.aliases();
        while (enumeration.hasMoreElements()) {
            String alias = (String) enumeration.nextElement();
            System.out.println("---- keyStore alias --- ：" + alias);
            X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
            System.out.println(" --- certificate ：" + certificate);
            Key privateKey = keyStore.getKey(alias, null);
            System.out.println(" --- privateKey ：" + privateKey);
            System.out.println("-----------------------------------------------\n\n ");
        }


        // AES key
//        SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
//        Key key = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");
//
//        keyStore.setKeyEntry("AA", key, "2wsx".toCharArray(), null);
//        keyStore.store(null); //this gives me the exception.




//        KeyPair kp = generateKeyPair();
//        X509Certificate cert  = caCertGen(kp);
//        System.out.println(cert);
//        savePrivateKey(kp.getPrivate(),"cert/privSm2.pri");
//        String pemCertString = saveX509ToPemFile(cert,"cert/certSm2.crt");
//        System.out.println(pemCertString);
//
//
//
//
//
//
//        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
//
////        kpGen.initialize(1024, SecureRandom.getInstance(p) );
//
//        KeyPair          pair = kpGen.generateKeyPair();
//
//        PKCS10CertificationRequest request = generateRequest(pair);
//
////        JcaPEMWriter        pemWrt = new PEMWriter(new OutputStreamWriter(System.out));
////
////        pemWrt.writeObject(request);
////
////        pemWrt.close();



    }



}
